using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Memory;
using Microsoft.Extensions.DependencyInjection;
using Stee.CAP8.Communication.HttpInterface;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace SampleHttpClient
{
    class Program
    {
        private static IHttpClientTest _httpClientProxy;

        static void Main(string[] args)
        {
            GetInstance();
            //Sync invoke
            CallAPISample();

            ////Async invoke
          //  CallAPISampleAsync().Wait();
            Console.ReadLine();
        }

        static void GetInstance()
        {
            if (_httpClientProxy == null)
            {
                IConfiguration configuration = ConfigurationManager.GetSection();

                //Get Instance by static function
                _httpClientProxy = HttpClientProxy.CreateInstance<IHttpClientTest>(configuration.GetSection("HttpClientOption"));

                //Get instance by inject
                //var services = new ServiceCollection();
                //services.AddHttpClientProxyService<IHttpClientTest>(configuration.GetSection("HttpClientOption"));
                //var provider = services.BuildServiceProvider();
                //_httpClientProxy = provider.GetService<IHttpClientTest>();           
            }
        }

        static void CallAPISample()
        {
            //Get List
            Console.WriteLine("Initial List");
            var httpList = _httpClientProxy.Get();
            foreach (var item in httpList)
            {
                Console.WriteLine($"ID: {item.ID}, Name: {item.Name}, CreateTime: {item.CreateTime}");
            }
            Console.WriteLine("");
            //Get Object
            var httpTest = _httpClientProxy.Get(2);
            Console.WriteLine("Get object ID=2");
            Console.WriteLine($"ID: {httpTest.ID}, Name: {httpTest.Name}, CreateTime: {httpTest.CreateTime}");
            Console.WriteLine("");

            //Post Object
            int order = httpList.Max(q => q.ID) + 1;
            var httpTestNew = new HttpTest() { ID = order, Name = $"name{order}", CreateTime = DateTime.Now.AddDays(4) };
            _httpClientProxy.Post(httpTestNew);

            //set Token and clientId
            _httpClientProxy.SetToken("token");
            

            //Put Object
            httpTest.Name = "name22";
            httpTest.CreateTime = DateTime.Now.AddHours(2);
            _httpClientProxy.Put(2, httpTest);

            //Delete Object
            _httpClientProxy.Delete(3);

            _httpClientProxy.SetRetryFlag();
            //print newest list
            Console.WriteLine("The newest list:");
            httpList = _httpClientProxy.Get();
            foreach (var item in httpList)
            {
                Console.WriteLine($"ID: {item.ID}, Name: {item.Name}, CreateTime: {item.CreateTime}");
            }
 
        }

        static async Task CallAPISampleAsync()
        {
            //Get List
            Console.WriteLine("Initial List");
            var httpList = await _httpClientProxy.GetAsync();
            foreach (var item in httpList)
            {
                Console.WriteLine($"ID: {item.ID}, Name: {item.Name}, CreateTime: {item.CreateTime}");
            }
            Console.WriteLine("");
            //Get Object
            var httpTest =await _httpClientProxy.GetAsync(2);
            Console.WriteLine("Get object ID=2");
            Console.WriteLine($"ID: {httpTest.ID}, Name: {httpTest.Name}, CreateTime: {httpTest.CreateTime}");
            Console.WriteLine("");

            //Post Object
            int order = httpList.Max(q => q.ID) + 1;
            var httpTestNew = new HttpTest() { ID = order, Name = $"name{order}", CreateTime = DateTime.Now.AddDays(4) };
            await _httpClientProxy.PostAsync(httpTestNew);
            Console.WriteLine("add object ID=4");
            Console.WriteLine("");

            //Put Object
            httpTest.Name = "name28";
            httpTest.CreateTime = DateTime.Now.AddHours(2);
            await _httpClientProxy.PutAsync(2, httpTest);
            Console.WriteLine("Update object ID=2");
            Console.WriteLine("");

            //Delete Object
            await _httpClientProxy.DeleteAsync(3);
            Console.WriteLine("Delete object ID=3");
            Console.WriteLine("");

            //print newest list
            Console.WriteLine("The newest list:");
            httpList = await _httpClientProxy.GetAsync();
            foreach (var item in httpList)
            {
                Console.WriteLine($"ID: {item.ID}, Name: {item.Name}, CreateTime: {item.CreateTime}");
            }
        }
    }
}
